package SystemB;

import Alarms.AlarmController;
import EventPackage.Participant;

public class FireAlarmController extends Participant {
	
	private static final int armDisarmAlarmEvtId = 7;
	private static final int confirmationArmDisarmEvtId = -7;
	private static final int armDisarmEvtId = 9;
	
	public FireAlarmController(String name, int subSystemHaltEnvId, String sensor_name) {
		super();
		
		this.name = name;
		mwXpos = 0; 
		mwYpos = 0; 
		mwHeight = 198;
		mwWidth = 550;
		setParameters(name, mwXpos, mwYpos, mwHeight, mwWidth, subSystemHaltEnvId, sensor_name);
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		FireAlarmController controller = new FireAlarmController("Fire Alarm Controller", 91, "Fire Alarm Controller");
		
		controller.run(args);
	}


	@Override
	protected void customHalt() {
		// TODO Auto-generated method stub
		
	}


	@Override
	protected void initialPostPhase() {
		// TODO Auto-generated method stub
		
	}


	@Override
	protected void handlePhase() {
		if (Evt.GetEventId() == armDisarmEvtId )
		{
			if (Evt.GetMessage().equals("1")) {
				arm("FA");
			} else {
				disarm("FA");
			}
		} // if
		
		if ( Evt.GetEventId() == confirmationArmDisarmEvtId )
		{
			String evtMsg = Evt.GetMessage();
			String alarmId = evtMsg.substring(0, evtMsg.length() - 1);
			char evtCommand = evtMsg.charAt(evtMsg.length() - 1);
			if (evtCommand == '1') {
				mw.WriteMessage("Arming Alarm: [" + alarmId + "] Armed");
			} else {
				mw.WriteMessage("Arming Alarm: [" + alarmId + "] Disarmed");
			}
			
		}
	}

	private void arm(String alarmId) {
		mw.WriteMessage("Trying to arm alarm: [" + alarmId + "]");
		postEvent(armDisarmAlarmEvtId, alarmId + "1");
	}
	
	private void disarm(String alarmId) {
		mw.WriteMessage("Trying to disarm alarm: [" + alarmId + "]");
		postEvent(armDisarmAlarmEvtId, alarmId + "0");
	}
	
	@Override
	protected void finalPostPhase() {
		// TODO Auto-generated method stub
		
	}


	@Override
	protected void beforeLooping() {
		// TODO Auto-generated method stub
		
	}

}
